package com.gengao.springbootcase.service.impl;

import com.gengao.springbootcase.mapper.EmpMapper;
import com.gengao.springbootcase.pojo.Emp;
import com.gengao.springbootcase.pojo.PageBean;
import com.gengao.springbootcase.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@Service
@Primary
//当存在多个相同类型的bean注入时，加上@primary注解，来确定默认的实现
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
//    @Override
//    public PageBean page(Integer page, Integer pageSize,String name, short gender, LocalDate begin,LocalDate end) {
////        获取总记录数目
//        long count=empMapper.count();
////        获取结果列表
//
//        Integer start=(page-1)*pageSize;
//        List<Emp> empList=empMapper.page(start,pageSize);
////        封装pageBean对象
//        return  new PageBean(count,empList);
//    }

    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {

//        1 引入依赖
//        2 设置分页参数
//        3 执行查询
//        4 封装pageBead对象

//
//        设置分页参数
        PageHelper.startPage(page,pageSize);
//        执行查询

        List<Emp> empList=empMapper.list(name,gender,begin,end);
        Page<Emp> p=(Page<Emp>) empList;
//        封装pageBean对象
        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
        return  pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        log.info("emp:{}",emp);
        empMapper.insert(emp);

    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        System.out.println("进来了service");
        return empMapper.getByUsernameAndPassword(emp);
    }
}
